package com.dude.utils;

import com.dude.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundPlayer 
{
	private static final int numOfStreams = 8;
	

	private Context context;
	private SoundPool soundPool;
	private float volume;
	

	// ============== singleton constructors =============
	private static SoundPlayer instance;

	public SoundPlayer getInstance(Context context){
		if (instance != null)
			return instance;
		else
			return new SoundPlayer();
	}
	
	private SoundPlayer()
	{
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		final float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		final float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//		volume = actualVolume / maxVolume;
		volume = maxVolume;
		
		soundPool =  new SoundPool(numOfStreams, AudioManager.STREAM_MUSIC, 0);
	}
	
	// ================== API calls ===============
	
	public void dispose()
	{
		if (soundPool != null)
    	{
    		soundPool.release();
    		soundPool = null;
    	}
	}
	
	public void play(int soundID)
	{
		soundPool.play(soundID, volume, volume, 1, 0, 1.0f);
	}
	
	public int load(String path)
	{
		if (path == null)
			return soundPool.load(context, R.raw.sound, 1);
		else
			return soundPool.load(path, 1);
		
	}
}
